package com.suncaper.han_yi_system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suncaper.han_yi_system.pojo.dto.ResetPasswordDto;
import com.suncaper.han_yi_system.pojo.dto.UserDto;
import com.suncaper.han_yi_system.pojo.dto.UserQueryDto;
import com.suncaper.han_yi_system.pojo.entity.LoginUser;
import com.suncaper.han_yi_system.pojo.vo.UserVo;
import com.suncaper.han_yi_system.result.Result;
import com.suncaper.han_yi_system.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "*")
@Tag(name = "用户管理")
@Slf4j
public class UserController {

    @Autowired
    UserService userService;

    @Operation(summary = "新增用户信息")
    @PostMapping("/save")
    public Result save(@RequestBody UserDto userDto){
        userService.saveUser(userDto);
        return Result.ok();
    }

    @Operation(summary = "更新用户信息")
    @PutMapping("/update")
    public Result update(@RequestBody UserDto userDto){
        userService.updateUser(userDto);
        return Result.ok();
    }

    @Operation(summary = "删除用户信息")
    @DeleteMapping("/delete")
    public Result delete(Long id){
        if(Objects.isNull(id)){
            return Result.error().message("id不能为空");
        }
        userService.removeUser(id);
        return Result.ok();
    }

    @Operation(summary = "根据id获取用户信息")
    @GetMapping("/getById")
    public Result getById(Long id){
        if(Objects.isNull(id)){
            return Result.error().message("id不能为空");
        }
        return Result.ok().data("user", userService.getById(id));
    }

    @Operation(summary = "获取用户列表")
    @RequestMapping("/list")
    public Result getList(){
        return Result.ok().data("userList", userService.list());
    }

    @Operation(summary = "分页获取用户列表")
    @GetMapping("/page")
    public Result pageList(UserQueryDto userQueryDto){
        Page<UserVo> list = userService.pageList(userQueryDto);
        return Result.ok().data("records", list.getRecords()).data("total", list.getTotal());
    }
    @PostMapping("/resetPassword")
    public Result resetPassword(@RequestBody ResetPasswordDto userDto){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser)authentication.getPrincipal();
        Long userId = loginUser.getUser().getId();
        userDto.setId(userId);
        userService.resetPassword(userDto);
        return Result.ok();
    }

    @GetMapping("/getUserByRole")
    @Operation(summary = "根据角色获取用户列表")
    public Result getUserByCollege(){
        List<UserVo> userList = userService.getUserByRole();
        return Result.ok().data("userList", userList);
    }


    @GetMapping("/getCurrentUser")
    @Operation(summary = "获取当前用户信息")
    public Result getCurrentUser(){
        UserVo currentUser = userService.getCurrentUser();
        return Result.ok().data("user", currentUser);
    }

}
